<?php
class troop
{
	// as a private function
	function appendImageRealUrl($item_array)
	{
		if (!is_array($item_array))
			return false;

		$ret_array = array();
		foreach ($item_array as $item)
		{
			if ($item = item::appendImageRealUrl($item))
			{
				$ret_array[] = $item;
			}
		}
		
		return $ret_array;

	}
	
	

	function getAllItemTroop($limit=50)
	{
		$result = sys::sql("select * from items order by item_id desc limit %s;", $limit);
		$result = self::appendImageRealUrl($result);
		return $result;
	}

	function getUserItemTroop($user_id, $start=0, $limt=20)
	{
		$result = sys::sql("select * from items where user_id='%d' order by item_id desc limit %d, %d;",
			$user_id, $start, $limt);
		$result = self::appendImageRealUrl($result);
		return $result;
	}
	
	function getUserItemTroopCount($user_id)
	{
		$result = sys::sql("select count(*) as count from items where user_id='%d';",
			$user_id);
		
		return intval($result[0]['count']);
	}
	function getUserLikeItemTroop($user_id, $start=0, $limt=20)
	{
		$result = sys::sql("select item_id from item_rates where "
					."user_id='%d' and rate=%d "
					."order by rate_id desc limit %d, %d;",
					$user_id,  ITEM_RATE_YES, $start, $limt
					);
		$item_id_list = array();
		for ($i=0; $i<count($result); $i++)
		{
			$item_id_list[] = $result[$i]['item_id'];
		}
		
		$result = sys::sql("select * from `items` where `item_id` in ('" . implode("','", $item_id_list) . "')");
		$result = self::appendImageRealUrl($result);
		return ($result);
		/*return sys::sql("select * from items where user_id='%d' order by item_id desc limit %d, %d;",
			$user_id, $start, $limt);*/

	}
	
	function getUserLikeItemTroopCount($user_id)
	{
		$result = sys::sql("select count(*) as count from item_rates where "
				."user_id='%d' and rate=%d;",
				$user_id,  ITEM_RATE_YES
				);
		
		return intval($result[0]['count']);
	}

}